const Base = require('./base.js');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.delivery = this.model('store/delivery');
		this.express = this.model('store/express');
		this.order_goods = this.model('store/order_goods');
	}
	async indexAction() {
		if(this.isGet) {
			let express = await this.express.select();
			this.assign("express", express);
			console.log(express)
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.userid)) {
			where.user_id = posts.userid;
		}
		const data = await this.delivery.dataTable(posts, where);
		console.log(JSON.stringify(data))
		return this.json(data);
	}

	async detailAction() {
		if(this.isGet) {
			let gets = this.get();
			let delivery = await this.delivery.findOne(gets.id);
			if(!think.isEmpty(delivery.order_goods_id)) {
				delivery.goods = await this.order_goods.where({
					id: delivery.order_goods_id
				}).select();
			} else {
				delivery.goods = await this.order_goods.where({
					order_id: delivery.order_id
				}).select();
			}

			this.assign("delivery", delivery);
			return this.display();
		}
	}
	async deliveryAction() {
		let posts = this.post();
		let user = await this.session("user");
		await this.delivery.setOne(posts.id, {
			express_id: posts.express_id,
			express_no: posts.express_no,
			status: 1,
			admin_id: user.id,
			delivery_time: new Date().valueOf()
		});
		return this.success();

	}
};